﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using DatabaseCompare.Model;
using DatabaseCompare.Service.Compare;

namespace DatabaseCompare.ServiceImpl.Compare
{


    public class ViewListCompare : ICompareAbleListCompare<ViewInfo, ViewCompareResult>
    {
        /// <summary>
        /// 表的比较类.
        /// </summary>
        private ICompareAbleCompare<ViewInfo, ViewCompareResult> viewCompare = new ViewCompare();


        public List<ViewCompareResult> Compare(List<ViewInfo> leftViewInfoList, List<ViewInfo> rightViewInfoList)
        {
            // 结果视图表.
            List<ViewCompareResult> resultList = new List<ViewCompareResult>();

            // 为避免下面的 Linq 处理异常
            // 假如 一个表的  视图 的视图表为 null
            // 将其设置为一个 空的视图表.
            if (leftViewInfoList == null)
            {
                leftViewInfoList = new List<ViewInfo>();
            }
            if (rightViewInfoList == null)
            {
                rightViewInfoList = new List<ViewInfo>();
            }


            // 使用 Linq 
            // 检索两个表的 视图 的并集。
            // 同时合并掉 相同的视图.
            var query =
                (from view in leftViewInfoList
                 select view.Name
                    ).Union
                (from view in rightViewInfoList
                 select view.Name
                    );


            // 获取 视图名 List.
            List<string> viewNameList = query.ToList();

            // 视图名排序.
            viewNameList.Sort();


            // 依次比较视图.
            for (int i = 0; i < viewNameList.Count; i++)
            {
                // 检索左边的视图.
                var queryLeft =
                    from data in leftViewInfoList
                    where data.Name == viewNameList[i]
                    select data;

                // 检索右边的视图.
                var queryRight =
                    from data in rightViewInfoList
                    where data.Name == viewNameList[i]
                    select data;


                // 视图的比较结果.
                ViewCompareResult viewCompareResult = null;

                if (queryLeft.Count() == 0)
                {
                    if (queryRight.Count() == 0)
                    {
                        // 左表找不到视图， 右表也找不到视图.
                        // 理论上这种情况不会发生.
                        viewCompareResult = viewCompare.Compare(null, null);
                    }
                    else
                    {
                        // 左表找不到视图， 右表找到视图.
                        viewCompareResult = viewCompare.Compare(null, queryRight.First());
                    }
                }
                else
                {
                    if (queryRight.Count() == 0)
                    {
                        // 左表找到视图， 右表找不到视图.
                        viewCompareResult = viewCompare.Compare(queryLeft.First(), null);
                    }
                    else
                    {
                        // 左表找到视图， 右表找到视图.
                        viewCompareResult = viewCompare.Compare(queryLeft.First(), queryRight.First());
                    }
                }


                // 将 视图的比较结果加入最后的比较视图表.
                resultList.Add(viewCompareResult);

            }

            return resultList;
        }
    }
}
